package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


import java.util.stream.Collectors
        ;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlan selectNursingPlanById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        // 1. 保护计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        this.save(nursingPlan);

        // 2. 保存计划项目信息
        // 2.1 设置计划项目计划id
        nursingPlanDto.getProjectPlans().forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        });
        boolean flag = nursingProjectPlanService.saveBatch(nursingPlanDto.getProjectPlans());
        return flag ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        // 1.修改基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        this.updateById(nursingPlan);

        boolean flag = false;
        // 2.修改项目计划信息
        if (ObjectUtil.isNotEmpty(nursingPlanDto.getProjectPlans())) {
            // 2.1 删除原项目计划信息
            nursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>()
                    .eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            // 2.2 保存项目计划信息
            flag = nursingProjectPlanService.saveBatch(nursingPlanDto.getProjectPlans());
        }

        return flag ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanByIds(Long[] ids) {
        // 1. 删除计划项目信息
        nursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>()
                .in(NursingProjectPlan::getPlanId, ids));
        // 2. 删除计划信息
        boolean flag = this.removeByIds(List.of(ids));
        return flag ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 根据id查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        // 1. 根据id查询护理计划
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        // 2. 根据护理计划id查询护理计划项目计划列表
        List<NursingProjectPlanVo> list = nursingProjectPlanService.lambdaQuery().eq(NursingProjectPlan::getPlanId, id).list().stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            BeanUtils.copyProperties(nursingProjectPlan, nursingProjectPlanVo);
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }
}
